home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / snip9707.zip / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  185 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  PushDir() and PopDir()
  5. **
  6. **  Original Copyright 1988-1991 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. */
  13.  
  14. #include <dos.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "dosfiles.h"
  19. #if defined(MSDOS) || defined(__MSDOS__)
  20.  #include "unistd.h"
  21. #else
  22.  #include <unistd.h>
  23. #endif
  24.  
  25. #define DIR_STACK_SIZE  8
  26. #define MAX_FLEN        67
  27.  
  28. static int  PushDir_stack_ptr;
  29. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  30.  
  31. /*
  32. **  PushDir()
  33. **
  34. **  Like chdir(), except a drive may be specified and the old directory
  35. **  is saved.
  36. **
  37. **  Arguments: 1 - newdir, the buffer containing the new directory name
  38. **
  39. **  Returns:  -1 - stack overflow
  40. **             0 - error
  41. **             1 - success, still on same drive
  42. **             2 - success, changed drive
  43. **
  44. **  Side effects: Converts name in newdir to upper case and prepends
  45. **                a drive letter.
  46. **
  47. **  CAUTION: Since a drive will be prepended to newdir, its buffer
  48. **           should be at least MAX_FLEN long.
  49. */
  50.  
  51. int PushDir(char *newdir)
  52. {
  53.       char pname[MAX_FLEN];
  54.       char drive[3];
  55.       char *target = &pname[2];
  56.       int  new_drv = 0, ercode = 0;
  57.       static int init = 0;
  58.  
  59.       if (!init)
  60.             PushDir_stack_ptr = init = -1;
  61.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  62.       {
  63.             ercode = -1;
  64.             goto ErrEx;
  65.       }
  66.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  67.       strupr(PushDir_stack[PushDir_stack_ptr]);
  68.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  69.       drive[2] = '\0';
  70.       if (':' == newdir[1])
  71.       {     /* If a drive is specified                                  */
  72.             strupr(newdir);
  73.             strcpy(pname, newdir);
  74.             if (strchr(target, ':'))      /* if filename is illegal     */
  75.                   goto ErrEx;
  76.             if (*drive != *newdir)
  77.             {
  78.                   if (Error_ == chdrv(newdir[0] - 'A'))
  79.                   {     /* If the drive is invalid                      */
  80.                         goto ErrEx;
  81.                   }
  82.                   else  new_drv = 1;
  83.             }
  84.       }
  85.       else
  86.       {     /* If a drive isn't specified                               */
  87.             if (!strchr(strupr(newdir), ':'))
  88.             {     /* If legal filename                                  */
  89.                   strcpy(pname, drive);
  90.                   strcat(pname, newdir);
  91.                   strcpy(newdir, pname);
  92.             }
  93.             else
  94.             {     /* If filename is illegal                             */
  95.                   goto ErrEx;
  96.             }
  97.       }
  98.  
  99.       if (*target)
  100.       {
  101.             if (chdir(target))
  102.             {
  103.                   if (1 == new_drv) /* We already changed drives        */
  104.                         chdrv(*drive - 'A');    /* Go home before exit  */
  105.                   goto ErrEx;
  106.             }
  107.       }
  108.       return (new_drv + 1);
  109. ErrEx:
  110.       --PushDir_stack_ptr;
  111.       return (ercode);
  112. }
  113.  
  114. /*
  115. **  PopDir()
  116. **
  117. **  Like chdir(), except goes to the drive/directory specified on the
  118. **  top of the PushDir stack.
  119. **
  120. **  Arguments: none
  121. **
  122. **  Returns:  -1 - stack empty
  123. **             0 - error - stack pointer unchanged
  124. **             1 - success, still on same drive
  125. **             2 - success, changed drive
  126. **
  127. **  Side effects: none
  128. **
  129. **  CAUTION: chdir() or chdrv() should not be called between PushDir-
  130. **           PopDir calls.
  131. */
  132.  
  133. int PopDir(void)
  134. {
  135.       char I_am_here[MAX_FLEN], target_drv, *target;
  136.       int new_drv = 0;
  137.  
  138.       if (0 > PushDir_stack_ptr)
  139.             return -1;
  140.       getcwd(I_am_here, MAX_FLEN);
  141.       target = &PushDir_stack[PushDir_stack_ptr][2];
  142.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  143.       if (I_am_here[0] != target_drv)
  144.       {
  145.             if (Error_ == chdrv(target_drv - 'A'))
  146.                   return 0;
  147.             new_drv = 1;
  148.       }
  149.       if (!chdir(target))
  150.       {
  151.             --PushDir_stack_ptr;
  152.             return (1 + new_drv);
  153.       }
  154.       else  return 0;
  155. }
  156.  
  157. /*
  158. **  isdir()
  159. **
  160. **  Checks to see if a drive and/or path are a valid directory.
  161. **
  162. **  Arguments: 1 - dir, the buffer containing the new directory name
  163. **
  164. **  Returns: Error_ - push/popdir stack overflow
  165. **           False_ - not a valid directory
  166. **           True_  - valid directory
  167. **
  168. **  Side effects: Converts name in dir to upper case and prepends a
  169. **                drive letter.
  170. **
  171. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  172. **           should be at least MAX_FLEN long.
  173. */
  174.  
  175. int isdir(char *dir)
  176. {
  177.       int ercode;
  178.  
  179.       if (-1 == (ercode = PushDir(dir)))
  180.             return ercode;
  181.       if (ercode)
  182.             PopDir();
  183.       return TOBOOL(ercode);
  184. }
  185.